package info_ctrl

import (
	"net/http"
	"ubt-aio-service/app/mod/info_mod"
	"ubt-aio-service/app/mod/sys_mod"
	"ubt-aio-service/app/pkg/parse"
	"ubt-aio-service/app/serv/info_serv"
	"ubt-aio-service/app/serv/sys_serv"
	"ubt-aio-service/app/utils"
)

type infor struct{}

var InfoCtrl infor

// Save 保存资讯
func (c *infor) Save(r *http.Request, w http.ResponseWriter) {
	// 通过头部请求的token获取用户信息
	token := r.Header.Get(utils.Token)
	userInfo := sys_serv.GetUserInfo(token)
	if userInfo == nil {
		utils.ToJson(w, utils.NoLogin)
		return
	}
	req := &info_mod.InfoSaveReq{}
	// 将请求的参数转换为请求结构体
	if err := parse.Parse(r, req); err != "" {
		utils.ToJson(w, &utils.ResJson{Code: utils.ParamErr.Code, Msg: err})
		return
	}
	// 过滤关键参数值中的非法字符
	req.InfoId = utils.InputFilter(req.InfoId, true)
	req.FstTitle = utils.InputFilter(req.FstTitle, false)
	req.SecTitle = utils.InputFilter(req.SecTitle, false)
	req.InfoType = utils.InputFilter(req.InfoType, true)
	req.Src = utils.InputFilter(req.Src, true)
	req.Describ = utils.TextFilter(req.Describ)
	req.FstImgId = utils.InputFilter(req.FstImgId, true)
	req.FstImgName = utils.InputFilter(req.FstImgName, true)
	req.FstImgUrl = utils.InputFilter(req.FstImgUrl, true)
	req.ImgIds = utils.InputFilter(req.ImgIds, true)
	req.ImgNames = utils.InputFilter(req.ImgNames, true)
	req.ImgUrls = utils.InputFilter(req.ImgUrls, true)
	req.FileIds = utils.InputFilter(req.FileIds, true)
	req.FileNames = utils.InputFilter(req.FileNames, true)
	req.FileUrls = utils.InputFilter(req.FileUrls, true)
	req.TagIds = utils.InputFilter(req.TagIds, true)
	req.TagNames = utils.InputFilter(req.TagNames, true)
	// 判断编码是否重复
	extVo := &info_mod.InfoExtVo{InfoId: req.InfoId, FstTitle: req.FstTitle, InfoType: req.InfoType}
	if ext := info_serv.InfoServ.Exists(extVo); ext {
		utils.ToJson(w, utils.Exists)
		return
	}
	var (
		res  *utils.ResJson
		kind uint8
	)
	if req.InfoId == "" {
		// 新增
		res = info_serv.InfoServ.Add(req)
		kind = 1
	} else {
		// 修改
		res = info_serv.InfoServ.Update(req)
		kind = 3
	}
	if res.Code == utils.Succeed.Code {
		logReq := &sys_mod.SysLogSaveReq{UserId: userInfo.UserId, MenuCode: utils.Infor, IpAddr: userInfo.ClientIp, Kind: kind}
		sys_serv.SysLogServ.Add(logReq)
	}
	utils.ToJson(w, res)
}

// Process 删除资讯
func (c *infor) Process(r *http.Request, w http.ResponseWriter) {
	// 通过头部请求的token获取用户信息
	token := r.Header.Get(utils.Token)
	userInfo := sys_serv.GetUserInfo(token)
	if userInfo == nil {
		utils.ToJson(w, utils.NoLogin)
		return
	}
	req := &info_mod.InfoProReq{}
	// 将请求的参数转换为请求结构体
	if err := parse.Parse(r, req); err != "" {
		utils.ToJson(w, &utils.ResJson{Code: utils.ParamErr.Code, Msg: err})
		return
	}
	// 过滤关键参数值中的非法字符
	req.InfoIds = utils.InputFilter(req.InfoIds, true)
	res := info_serv.InfoServ.Process(req)
	if res.Code == utils.Succeed.Code {
		var kind uint8
		switch req.States {
		case 1:
			kind = 4
		case 3:
			kind = 5
		case 2:
			kind = 2
		default:
			kind = 2
		}
		logReq := &sys_mod.SysLogSaveReq{UserId: userInfo.UserId, MenuCode: utils.Infor, IpAddr: userInfo.ClientIp, Kind: kind}
		sys_serv.SysLogServ.Add(logReq)
	}
	utils.ToJson(w, res)
}

// One 获取资讯详情
func (c *infor) One(r *http.Request, w http.ResponseWriter) {
	// 通过头部请求的token获取用户信息
	token := r.Header.Get(utils.Token)
	userInfo := sys_serv.GetUserInfo(token)
	if userInfo == nil {
		utils.ToJson(w, utils.NoLogin)
		return
	}
	// 过滤关键参数值中的非法字符
	u := r.URL.Query()
	if u == nil {
		utils.ToJson(w, utils.ParamErr)
		return
	}
	// 过滤关键参数值中的非法字符
	infoId := u.Get("infoId")
	infoId = utils.InputFilter(infoId, true)
	if infoId == "" || len(infoId) != 21 {
		utils.ToJson(w, utils.ParamErr)
		return
	}
	res := info_serv.InfoServ.One(infoId)
	utils.ToJson(w, res)
}

// Page 获取资讯分页列表
func (c *infor) Page(r *http.Request, w http.ResponseWriter) {
	// 通过头部请求的token获取用户信息
	token := r.Header.Get(utils.Token)
	userInfo := sys_serv.GetUserInfo(token)
	if userInfo == nil {
		utils.ToJson(w, utils.NoLogin)
		return
	}
	// 过滤关键参数值中的非法字符
	u := r.URL.Query()
	if u == nil {
		utils.ToJson(w, utils.ParamErr)
		return
	}
	kinds := u.Get("kind")
	keywords := u.Get("keywords")
	infoType := u.Get("infoType")
	statess := u.Get("states")
	begins := u.Get("beginTime")
	ends := u.Get("endTime")
	limits := u.Get("limit")
	offsets := u.Get("offset")
	kinds = utils.InputFilter(kinds, true)
	keywords = utils.InputFilter(keywords, false)
	infoType = utils.InputFilter(infoType, true)
	statess = utils.InputFilter(statess, true)
	begins = utils.InputFilter(begins, true)
	ends = utils.InputFilter(ends, true)
	limits = utils.InputFilter(limits, true)
	offsets = utils.InputFilter(offsets, true)
	kind := parse.Str2u8(kinds)
	states := parse.Str2u8(statess)
	beginDate := parse.Str2int(begins)
	endDate := parse.Str2int(ends)
	limit := parse.Str2u8(limits)
	offset := parse.Str2u32(offsets)
	// 先统计信息的数量
	req := &info_mod.InfoPageReq{Keywords: keywords, InfoType: infoType, Kind: kind, States: states, BeginDate: beginDate, EndDate: endDate}
	res := info_serv.InfoServ.Count(req)
	if res.Code == utils.Succeed.Code {
		dto := &info_mod.InfoPageRes{Total: res.Data.(map[string]any)["nums"].(int64)}
		// 再查询具体的信息列表
		req.Limit = limit
		req.Offset = offset
		if res = info_serv.InfoServ.Page(req); res.Code == utils.Succeed.Code {
			dto.List = res.Data
			utils.ToJson(w, &utils.ResJson{Code: res.Code, Msg: res.Msg, Data: dto})
			return
		}
	}
	utils.ToJson(w, utils.NoData)
}

// Sort 获取资讯数量，用于排序
func (c *infor) Sort(r *http.Request, w http.ResponseWriter) {
	// 通过头部请求的token获取用户信息
	token := r.Header.Get(utils.Token)
	userInfo := sys_serv.GetUserInfo(token)
	if userInfo == nil {
		utils.ToJson(w, utils.NoLogin)
		return
	}
	// 过滤关键参数值中的非法字符
	u := r.URL.Query()
	if u == nil {
		utils.ToJson(w, utils.ParamErr)
		return
	}
	infoType := u.Get("infoType")
	infoType = utils.InputFilter(infoType, true)
	if infoType == "" || len(infoType) != 21 {
		utils.ToJson(w, utils.ParamErr)
		return
	}
	res := info_serv.InfoServ.Sort(infoType)
	utils.ToJson(w, res)
}
